package com.srwl.mytx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.R;

import java.util.List;

public class ReasonsAdapter extends RecyclerView.Adapter<ReasonsAdapter.ReasonViewHolder> {
    private static final int ITEM_TYPE_BUYER = 0;
    private static final int ITEM_TYPE_SELLER = 1;
    private List<String> reasons;
    private Context context;


    public ReasonsAdapter(Context context, List<String> reasons) {
        this.reasons = reasons;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (reasons.get(position).startsWith("买家")) {
            return ITEM_TYPE_BUYER;
        } else {
            return ITEM_TYPE_SELLER;
        }
    }

    @Override
    public ReasonsAdapter.ReasonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_BUYER) {
            return new ReasonViewHolder(LayoutInflater.from(context).inflate(R.layout.reason_list_buyer_item, parent, false));
        } else {
            return new ReasonViewHolder(LayoutInflater.from(context).inflate(R.layout.reason_list_seller_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ReasonsAdapter.ReasonViewHolder holder, int position) {
        //因为添加原因的时候，为了区分是买家还是卖家给出的原因，加上了"买家："或者"卖家："的前缀，所以这儿需要分割一下
        String content = reasons.get(position).substring(3);
        holder.tv_content.setText(content);
    }

    @Override
    public int getItemCount() {
        return reasons == null ? 0 : reasons.size();
    }

    class ReasonViewHolder extends RecyclerView.ViewHolder {
        TextView tv_content;

        public ReasonViewHolder(View itemView) {
            super(itemView);
            tv_content = itemView.findViewById(R.id.tv_content);

        }
    }


}


